package com.foot.travel.activity;

import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.foot.travel.R;
import com.koalac.ironman.common.module.ModuleActivity;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.SmsSend;
import com.koalac.ironman.net.pojo.response.MessageCode;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.util.SMSUtil;
import com.koalac.ironman.util.ScreenInfoUtil;
import com.koalac.ironman.util.StringUtils;
import com.koalac.ironman.viewhelper.ViewInject;
import com.koalac.ironman.widget.LoadingDialog;

public class RegisterActivity extends ModuleActivity implements OnClickListener {

    @ViewInject(id = R.id.parent_layout)
    View mParentLayout;
    @ViewInject(id = R.id.moblie_num)
    EditText mMobileNumView;
    @ViewInject(id = R.id.verification)
    EditText mVerificationView;
    @ViewInject(id = R.id.time_count, click = "onClick")
    TextView mTimeCountView;
    @ViewInject(id = R.id.next_btn, click = "onClick")
    TextView mNextBtn;
    TimeCount mTimeCount;
    String mVerificationCode;
    
    public static final String OPEN_ID = "open_id";
    public static final String NICK_NAME = "nickname";
    public static final String HEADIMGURL = "headimgurl";
    private String mOpenId;
    private String nickname;
    private String headimgurl;

    public static final String MOBILE_KEY = "mobile_key";
    private final ContentObserver contentObserver = new ContentObserver(null) {
	@Override
	public void onChange(boolean selfChange) {
	    super.onChange(selfChange);
	    RegisterActivity.this.runOnUiThread(new Runnable() {

		@Override
		public void run() {
		    String vfCode = SMSUtil.getSmsyzm(RegisterActivity.this);
		    if (!StringUtils.isEmpty(vfCode)) {
			mVerificationView.setText(vfCode);
			mVerificationView.invalidate();
			mVerificationView.setSelection(vfCode.length());
		    }
		}
	    });

	}
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setNavigationTitle("注册");
	setContentLayoutBackgroudColor(getResources().getColor(
		R.color.module_color1));
	setBackButtonVisibility(true);
	mTimeCount = new TimeCount(60000, 1000);
	getContentResolver().registerContentObserver(
		Uri.parse("content://sms"), true, contentObserver);

	int bottomHeight = ScreenInfoUtil.getScreenHeight(RegisterActivity.this) - ScreenInfoUtil.dp2px(RegisterActivity.this, 410);
	
	checkKeyboardHeight(mParentLayout,bottomHeight);
	mOpenId = getIntent().getStringExtra(OPEN_ID);
	nickname = getIntent().getStringExtra(NICK_NAME);
	headimgurl = getIntent().getStringExtra(HEADIMGURL);
    }

    @Override
    protected void initLayout(ViewGroup rootContentView) {
	addContentView(R.layout.activity_register);
    }

    class TimeCount extends CountDownTimer {

	public TimeCount(long millisInFuture, long countDownInterval) {
	    super(millisInFuture, countDownInterval);
	}

	@Override
	public void onTick(long millisUntilFinished) {
	    mTimeCountView.setClickable(false);
	    mTimeCountView.setText(millisUntilFinished / 1000 + "s");
	}

	@Override
	public void onFinish() {
	    mTimeCountView.setText("获取验证码");
	    mTimeCountView.setClickable(true);

	}
    }

    private boolean isCorrectPhoneNum() {
	if (StringUtils.isEmpty(mMobileNumView.getText().toString())) {
	    toast("手机号为空！");
	    return false;
	}
	if (!StringUtils.isMobilePhone(mMobileNumView.getText().toString())) {
	    toast("请填写正确的手机号！");
	    return false;
	}
	return true;
    }

    private boolean checkInput() {
	if (!isCorrectPhoneNum()) {
	    return false;
	}
	
	if (StringUtils.isEmpty(mVerificationCode)) {
	    toast("请先获取验证码！");
	    return false;
	}
	if (StringUtils.isEmpty(mVerificationView.getText().toString())) {
	    toast("验证码为空！");
	    return false;
	}
	
	return true;
    }

    private void getVerificationCode() {
	SmsSend smsSend = new SmsSend();
	smsSend.setPhone(mMobileNumView.getText().toString());
	final LoadingDialog loadingDialog = showLoadingDialog("获取验证码...");
	MyRequest.request(getApplicationContext(), smsSend,
		new ResponseCallback() {

		    @Override
		    public void process(boolean result, Context context,
			    Response data, String msg) {
			canceledLoadingDialog(loadingDialog);
			if (result) {
			    MessageCode messageCode = data
				    .getObject(MessageCode.class);
			    mVerificationCode = messageCode.getMessage_code();
			}
		    }
		});
    }

    @Override
    public void onClick(View v) {
	int viewId = v.getId();
	switch (viewId) {
	case R.id.time_count:
	    if (isCorrectPhoneNum()) {
		mTimeCount.start();
		getVerificationCode();
		mTimeCountView.setSelected(true);
	    }
	    break;
	case R.id.next_btn:
	    if (checkInput()) {
		if (!mVerificationCode.equals(mVerificationView.getText()
			.toString())) {
		    toast("验证码有误!");
		    return;
		}
		/**
		 * 跳转到设置密码
		 */
		Intent intent = new Intent(RegisterActivity.this,
			SetPasswordActivity.class);
		intent.putExtra(MOBILE_KEY, mMobileNumView.getText().toString());
		String openId = getIntent().getStringExtra(OPEN_ID);
		if (!StringUtils.isEmpty(openId)) {
		    intent.putExtra(OPEN_ID, openId);
		}
		if (!StringUtils.isEmpty(nickname)) {
		    intent.putExtra(NICK_NAME, nickname);
		}
		if (!StringUtils.isEmpty(HEADIMGURL)) {
		    intent.putExtra(HEADIMGURL, headimgurl);
		}
		startActivity(intent);
	    }
	    break;
	}
    }

}
